"""
解析数据到智图和A表
"""
import os
import shutil
import string
import sys

import facade
from parsel import Selector
from xjlibrary.mdatetime.mtime import getTodayDate
from xjlibrary.mdatetime.mtime2 import MDateTimeUtils
from xjlibrary.our_file_dir import BaseDir
from xjlibrary.tools.BaseString import BaseString

from AuthorOrganSort import SortCreatorOrgan
from id_encode import GetLngid

curpath = BaseDir.get_file_dir_absolute(__file__)
TopPath = BaseDir.get_upper_dir(curpath, -2)
dirPath = BaseDir.get_new_path(TopPath, "download", "yuetongthesis", "download", "thesishtml")
db3dir = BaseDir.get_new_path(TopPath, "download", "yuetongthesis", "download", "db3")
BaseDir.create_dir(db3dir)
configfile = BaseDir.get_new_path(curpath, "db.ini")
sPath = BaseDir.get_new_path(curpath, "template")


class ParaZTA(object):
    def __init__(self):
        self.logger = facade.get_streamlogger()
        self.mysqlutils = facade.MysqlUtiles(configfile, "db", logger=self.logger)
        self.Listpara = []
        self.templatedb3 = BaseDir.get_new_path(sPath, "zt_template.db3")
        self.new_db3_path = self.copyTempDB3(self.templatedb3, db3dir)
        self.db3_zt = facade.Sqlite3Utiles(logger=self.logger).Sqlite3DBConnectFromFilePath(
            self.new_db3_path, encoding="utf-8")

    def copyTempDB3(self, temp_path, copy_out):
        """
        拷贝创建新的db3文件
        :param temp_path: 模板db3文件路径
        :param copy_out: 新的db3输出路径
        :return: 新db3路径
        """
        today_str = str(getTodayDate())
        if not os.path.isfile(temp_path) or not os.path.isdir(copy_out):
            raise FileNotFoundError
        new_db3_name = 'zt_yuetongthesis_' + today_str + '.db3'
        new_db3_path = os.path.join(copy_out, new_db3_name)
        shutil.copyfile(temp_path, new_db3_path)
        return new_db3_path

    def select_files(self):
        for file in BaseDir.get_dir_all_files(dirPath):
            print(file)
            rawid = file.split("\\")[-1].replace(".html", "")
            html = BaseDir.single_read_file(file)
            self.para_html(html, rawid)
        self.insert_db3()

    def insert_db3(self):
        sql = "insert or ignore into modify_title_info_zt (`lngid`,`rawid`,`title`,`creator`," \
              "`country`,`language`,`provider`,`batch`," \
              "`type`,`medium`,`provider_url`,`provider_id`,`date`," \
              "`date_created`,`price`,`description`,`publisher`," \
              "`creator_institution`,`subject`,`page`,`pagecount`,`identifier_pisbn`) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)"

        self.db3_zt.ExeSqlliteMany(sql, self.Listpara)
        self.Listpara.clear()

    def para_html(self, html, rawid):
        select = Selector(text=html)
        div_tag_list = select.xpath('.//div[@class="minbox"]/div[@class="minboxtit"]')
        title = ""
        author = ""
        en_author = ""
        pub_date = ""
        price = ""
        origan = ""
        keyword = ""
        abs = ""
        en_abs = ""
        publisher = ""
        pages = ""
        pisbn = ""
        for div_tag in div_tag_list:
            strings = "".join(div_tag.xpath(".//text()").getall())
            strings = strings.strip()
            if strings.find("【中文名称】：") > -1:
                title = strings.replace("【中文名称】：", "").strip()
            elif strings.find("【作者】：") > -1:
                author = strings.replace("【作者】：", "").strip()
                author = BaseString.cleanSemicolon(author)
            elif strings.find("【英文作者】：") > -1:
                # 只有一个 还是错的  不要
                en_author = strings.replace("【英文作者】：", "").strip()
            elif strings.find("【出版时间】：") > -1:
                pub_date = strings.replace("【出版时间】：", "").strip()
            elif strings.find("【价格】：") > -1:
                price = strings.replace("【价格】：", "").strip()
            elif strings.find("【作者单位】：") > -1:
                origan = strings.replace("【作者单位】：", "").strip()
                origan = BaseString.cleanSemicolon(origan)
            elif strings.find("【关键词】：") > -1:
                keyword = strings.replace("【关键词】：", "").strip()
            elif strings.find("【摘要】：") > -1:
                abs = strings.replace("【摘要】：", "").strip()
            elif strings.find("【英文摘要】：") > -1:
                en_abs = strings.replace("【英文摘要】：", "").strip()
            elif strings.find("【出版地点】：") > -1:
                publisher = strings.replace("【出版地点】：", "").strip()
            elif strings.find("【页数】：") > -1:
                pages = strings.replace("【页数】：", "").strip()
            elif strings.find("【备注】：") > -1:
                pisbn = strings.replace("【备注】：", "").strip()
            elif strings.find("您尚未购买此资源") > -1:
                pass
            else:
                self.logger.info(strings)
                sys.exit("出现未解析的字段,停止程序检查")

        sub_db_id = "00133"
        product = "YUETONG"
        sub_db = "XW"
        provider_zt = "yuetongthesis"
        provider = "CCPCL"

        bacth = MDateTimeUtils.get_today_date_strings() + "00"
        type_ = "4"
        medium = "2"

        country = "CN"
        language = "ZH"

        url = "http://zt.yuetong.cn/rmjt/fg/article/otherarticle.html?sign=detail&articleid={}".format(rawid)
        provider_url = provider_zt + "@" + url
        provider_id = provider_zt + "@" + rawid

        if pub_date:
            pub_date = pub_date.split(" ")[0]
            pub_date = pub_date.strip()

            def pub_date_deal(sub):
                list_pub = pub_date.split(sub)
                if len(list_pub) > 1:
                    if len(list_pub[1]) == 1:
                        list_pub[1] = "0" + list_pub[1]
                    if len(list_pub[-1]) == 1:
                        list_pub[-1] = "0" + list_pub[-1]
                return "".join(list_pub)

            pub_date = pub_date_deal("/")
            pub_date = pub_date_deal("-")
            pub_date = pub_date_deal(".")
            if len(pub_date) == 6:
                pub_date = pub_date + "00"
            if len(pub_date) == 4:
                pub_date = pub_date + "0000"

            if len(pub_date) != 8:
                self.logger.info("{}:{}".format(rawid, pub_date))
                sys.exit(-1)
            pub_year = pub_date[:4]
        else:
            pub_year = "1900"
            pub_date = "19000000"

        if BaseString.hasnum(author):
            print(author)
            author_list = author.split(";")
            dicts = {}
            for one_author in author_list:
                for i in '123456789':
                    if one_author.find(str(i)) > -1:
                        if dicts.__contains__(str(i)):
                            one_author = one_author.replace(str(i), "")
                            dicts[str(i)].append(one_author)
                        else:
                            lists = []
                            one_author = one_author.replace(str(i), "")
                            lists.append(one_author)
                            dicts[str(i)] = lists
                        break

            if dicts:
                # 如果存在dicts 进行机构解析，否则判定为无编号
                print(dicts)
                dicts1 = {}
                origan_list = origan.split(";")
                for one_origan in origan_list:
                    for i in '123456789':
                        if one_origan.find(str(i)) > -1:
                            if dicts1.__contains__(str(i)):
                                one_origan = one_origan.replace(str(i), "").replace(".", "")
                                dicts1[str(i)].append(one_origan)
                            else:
                                lists = []
                                one_origan = one_origan.replace(str(i), "").replace(".", "")
                                lists.append(one_origan)
                                dicts1[str(i)] = lists
                            break
                print(dicts1)
                if dicts1:
                    try:
                        dictsall = {}
                        for key in dicts.keys():
                            for author in dicts[key]:
                                dictsall[author] = ";".join(dicts1[key])
                        print(dictsall)
                        author, origan = SortCreatorOrgan(dictsall)
                        print(author)
                        print(origan)
                    except:
                        pass
        pisbn = pisbn.replace("-", "")
        pagecount = pages
        publisher = "人民交通出版社"
        lngid = GetLngid(sub_db_id, rawid)
        # 不要这一条明显错误数据
        if rawid == "2c9081f757c0d5330157c0e933050027":
            return

        self.Listpara.append((lngid, rawid, title, author, country,
                              language, provider_zt, bacth, type_, medium,
                              provider_url, provider_id, pub_year, pub_date,
                              price, abs, publisher, origan, keyword, pages,
                              pagecount, pisbn))


if __name__ == "__main__":
    """
    作者机构的特殊情况
    http://zt.yuetong.cn/rmjt/fg/article/otherarticle.html?sign=detail&articleid=liuyandaorulunwenAAAAAAAAAAAA404
    http://zt.yuetong.cn/rmjt/fg/article/otherarticle.html?sign=detail&articleid=liuyandaorulunwenAAAAAAAAAAAA354
    正常情况
    http://zt.yuetong.cn/rmjt/fg/article/otherarticle.html?sign=detail&articleid=liuyandaorulunwenAAAAAAAAAAA1827
    http://zt.yuetong.cn/rmjt/fg/article/otherarticle.html?sign=detail&articleid=liuyandaorulunwenAAAAAAAAAAA1010
    http://zt.yuetong.cn/rmjt/fg/article/otherarticle.html?sign=detail&articleid=liuyandaorulunwenAAAAAAAAAAA1001
    本库不解析a层
    """

    para = ParaZTA()
    para.select_files()
